package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.MigrateGovernProcess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移治理过程类-[表名: 迁移治理过程(migrate_govern_process)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateGovernProcessDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateGovernProcessDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateGovernProcess(MigrateGovernProcess migrateGovernProcess) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_govern_process ( "
                + "govern_proc_id,govern_task_id,govern_way,govern_way_desc,govern_incharge,govern_way_state,govern_date"
                + ") values ("+ "?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateGovernProcess.getGovernProcId());
            ps.setObject(2, migrateGovernProcess.getGovernTaskId());
            ps.setObject(3, migrateGovernProcess.getGovernWay());
            ps.setObject(4, migrateGovernProcess.getGovernWayDesc());
            ps.setObject(5, migrateGovernProcess.getGovernIncharge());
            ps.setObject(6, migrateGovernProcess.getGovernWayState());
            ps.setObject(7, migrateGovernProcess.getGovernDate());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateGovernProcess is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateGovernProcess migrateGovernProcess) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_govern_process where 1=1"
                + " AND govern_proc_id = ? "
                );
            ps.setObject(1, migrateGovernProcess.getGovernProcId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateGovernProcess is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateGovernProcess migrateGovernProcess) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_govern_process set "
                + " govern_proc_id = ? ,"
                + " govern_task_id = ? ,"
                + " govern_way = ? ,"
                + " govern_way_desc = ? ,"
                + " govern_incharge = ? ,"
                + " govern_way_state = ? ,"
                + " govern_date = ? "
                + " where 1=1"
                + " AND govern_proc_id = ? "
                );
            ps.setObject(1, migrateGovernProcess.getGovernProcId());
            ps.setObject(2, migrateGovernProcess.getGovernTaskId());
            ps.setObject(3, migrateGovernProcess.getGovernWay());
            ps.setObject(4, migrateGovernProcess.getGovernWayDesc());
            ps.setObject(5, migrateGovernProcess.getGovernIncharge());
            ps.setObject(6, migrateGovernProcess.getGovernWayState());
            ps.setObject(7, migrateGovernProcess.getGovernDate());
            ps.setObject(8, migrateGovernProcess.getGovernProcId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernProcess is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateGovernProcess queryByPk(MigrateGovernProcess migrateGovernProcess) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateGovernProcess result = null;
        try {
            ps = conn.prepareStatement("select govern_proc_id,govern_task_id,govern_way,govern_way_desc,govern_incharge,govern_way_state,govern_datefrom migrate_govern_process where 1=1 "
                + " AND govern_proc_id = ? "
                );
            ps.setObject(1, migrateGovernProcess.getGovernProcId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateGovernProcess();
				result.setGovernProcId(rs.getString("govern_proc_id"));
				result.setGovernTaskId(rs.getString("govern_task_id"));
				result.setGovernWay(rs.getString("govern_way"));
				result.setGovernWayDesc(rs.getString("govern_way_desc"));
				result.setGovernIncharge(rs.getString("govern_incharge"));
				result.setGovernWayState(rs.getString("govern_way_state"));
				result.setGovernDate(rs.getString("govern_date"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernProcess is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateGovernProcess 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateGovernProcess> queryAll(MigrateGovernProcess migrateGovernProcess) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateGovernProcess> result = new ArrayList<>();
        try {
            String sql = "select govern_proc_id,govern_task_id,govern_way,govern_way_desc,govern_incharge,govern_way_state,govern_datefrom migrate_govern_process where 1=1 ";
                if (migrateGovernProcess.getGovernProcId() != null) {
                    sql = sql + " AND govern_proc_id =  '" + migrateGovernProcess.getGovernProcId() + "'";
                }
                if (migrateGovernProcess.getGovernTaskId() != null) {
                    sql = sql + " AND govern_task_id =  '" + migrateGovernProcess.getGovernTaskId() + "'";
                }
                if (migrateGovernProcess.getGovernWay() != null) {
                    sql = sql + " AND govern_way =  '" + migrateGovernProcess.getGovernWay() + "'";
                }
                if (migrateGovernProcess.getGovernWayDesc() != null) {
                    sql = sql + " AND govern_way_desc =  '" + migrateGovernProcess.getGovernWayDesc() + "'";
                }
                if (migrateGovernProcess.getGovernIncharge() != null) {
                    sql = sql + " AND govern_incharge =  '" + migrateGovernProcess.getGovernIncharge() + "'";
                }
                if (migrateGovernProcess.getGovernWayState() != null) {
                    sql = sql + " AND govern_way_state =  '" + migrateGovernProcess.getGovernWayState() + "'";
                }
                if (migrateGovernProcess.getGovernDate() != null) {
                    sql = sql + " AND govern_date =  '" + migrateGovernProcess.getGovernDate() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateGovernProcess obj = new MigrateGovernProcess();
				obj.setGovernProcId(rs.getString("govern_proc_id"));
				obj.setGovernTaskId(rs.getString("govern_task_id"));
				obj.setGovernWay(rs.getString("govern_way"));
				obj.setGovernWayDesc(rs.getString("govern_way_desc"));
				obj.setGovernIncharge(rs.getString("govern_incharge"));
				obj.setGovernWayState(rs.getString("govern_way_state"));
				obj.setGovernDate(rs.getString("govern_date"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateGovernProcess is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
